package com.windoent.yugioh.help.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

/**
 * author : robert
 * date   : 2021/6/15 11:38
 * desc   :
 */
public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DB_NAME = "china_city.db";
    public static final String TB_NAME = "city";
    public static final String COLUMNS_ID = "id";
    public static final String COLUMNS_CODE = "code";
    public static final String COLUMNS_NAME = "name";
    public static final String COLUMNS_PINYIN = "pinyin";
    public static final String COLUMNS_CITY_ID = "city_id";
    public static final String COLUMNS_SORT = "sort";

    public DatabaseHelper(@Nullable Context context) {
        super(context, DB_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        // Attention:注意SQL语法，每个变量后需要有空格，否则不认识。
        String sql = "CREATE TABLE IF NOT EXISTS " + TB_NAME + " (" + COLUMNS_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMNS_CITY_ID
                + " INTEGER," + COLUMNS_NAME + " TEXT,"+ COLUMNS_SORT + " TEXT," + COLUMNS_PINYIN + " TEXT," + COLUMNS_CODE
                + " TEXT)";

        sqLiteDatabase.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+TB_NAME);
        onCreate(sqLiteDatabase);
    }
}
